package com.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.common.dto.PagingReqDTO;
import com.dao.QuAnswerMapper;
import com.entity.QuAnswer;
import com.entity.dto.QuAnswerDTO;
import com.service.QuAnswerService;
import com.utils.PageUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 语言设置 服务实现类
 * </p>
 *
 * @author 聪明笨狗
 * @since 2020-05-25 13:23
 */
@Service
public class QuAnswerServiceImpl extends ServiceImpl<QuAnswerMapper, QuAnswer> implements QuAnswerService {

    @Override
    public PageUtils paging(PagingReqDTO<QuAnswerDTO> reqDTO) {
        Page page = new Page();
        page.setCurrent(reqDTO.getCurrent());
        page.setSize(reqDTO.getSize());


        int pagingCount = baseMapper.pagingCount(reqDTO.getParams());
        page.setTotal(pagingCount);
        if (pagingCount > 0) {
            List<QuAnswer> paging = baseMapper.paging(reqDTO.getParams());
            page.setRecords(paging);
        } else {
            page.setRecords(Collections.emptyList());
        }
        return new PageUtils(page);
    }

    @Override
    public List<QuAnswer> listAnswerByRandom(String quId) {
        return this.baseMapper.selectList(new EntityWrapper<QuAnswer>()
                .eq("qu_id", quId));
    }

    @Override
    public List<QuAnswerDTO> listByQu(String quId) {
        List<QuAnswer> list = this.baseMapper.selectList(new EntityWrapper<QuAnswer>()
                .eq("qu_id", quId));
        if (!CollectionUtils.isEmpty(list)) {
            return list.stream().map(v -> {
                QuAnswerDTO dto = new QuAnswerDTO();
                BeanUtils.copyProperties(v, dto);
                return dto;
            }).collect(Collectors.toList());
        }

        return null;
    }


    /**
     * 查找已存在的列表
     *
     * @param quId
     * @return
     */
    public List<String> findExistsList(String quId) {
        //返回结果
        List<String> ids = new ArrayList<>();

        List<QuAnswer> list = this.baseMapper.selectList(new EntityWrapper<QuAnswer>()
                .eq("qu_id", quId));

        if (!CollectionUtils.isEmpty(list)) {
            for (QuAnswer item : list) {
                ids.add(item.getId());
            }
        }
        return ids;
    }

    @Override
    public void saveAll(String quId, List<QuAnswerDTO> list) {

        //最终要保存的列表
        List<QuAnswer> saveList = new ArrayList<>();

        //已存在的标签列表
        List<String> ids = this.findExistsList(quId);

        if (!CollectionUtils.isEmpty(list)) {
            for (QuAnswerDTO item : list) {

                //标签ID
                String id = item.getId();
                QuAnswer answer = new QuAnswer();
                BeanUtils.copyProperties(item, answer);
                answer.setQuId(quId);

                //补全ID避免新增
                if (ids.contains(id)) {
                    ids.remove(id);
                }

                saveList.add(answer);
            }

            //保存标签列表
            if (!CollectionUtils.isEmpty(saveList)) {
                this.insertOrUpdateBatch(saveList);
            }

            //删除已移除
            if (!ids.isEmpty()) {
                this.deleteBatchIds(ids);
            }
        } else {

            this.delete(new EntityWrapper<QuAnswer>()
                    .eq("qu_id", quId));
        }
    }


}
